
var licenseKeyFormatting = function(s, k) {
    s=s.split('-').join('').toUpperCase();
    console.log(s);
    let n=s.length;
    let ans=[];
    if(n%k==0){
    for(let i=0,j=0;i<n;){
     ans[j]=s.slice(i,i+k);
    i+=k;
    j++
}
}
else{
    let m=n%k;
    ans[0]=s.slice(0,m);
    for(let i=m,j=1;i<n;){
        ans[j]=s.slice(i,i+k);
        i+=k;
        j++;
    }
}
return ans.join('-');
};
var a=licenseKeyFormatting('2-5g-3-j',2);
console.log(a);
 
